<?php
/*
 * File: HookController.php
 * Project: NewDemo
 * Created Time: 2024-05-29 11:27:52 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-05-30 4:37:30 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2023 - 2024 成都睿創科技有限公司
 */



namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Tests\CreatesApplication;
use App\Http\Controllers\Controller;

class HookController extends Controller{


    /**
     * @param Request $request
     * @return array
     * https://api.bscdhk.com/api/Hook
     * git Hook
     */
    public function Hook(Request $request){
        $dir=dirname(base_path());
        exec("cd ".$dir." && sudo git pull origin master" ,$results );
        if ($results[0]=='Already up-to-date.') {
            return '提交过了';
        }else if(count($results)>3) {
            return '提交成功';
        }else{
            return '提交错误'; 
        }
    }


    /**
     * @param Request $request
     * @return array
     * https://api.bscdhk.com/api/curl_hook
     * git Hook
     */
    public function curl_hook(Request $request){
        $base_uri = config('services.basics.base_uri').'/api/Hook'; 
        $result = $this->curlPost($base_uri);
        if($result=='提交超时'){
            $result = $this->curlPost($base_uri);
        }
        return $result;
    }







    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     */
    private function curlPost($url,$data=null){

        $data  = json_encode($data);    
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec ( $curl );
        if (false == $ret) {
            // $result = curl_error( $curl);
            $result = '提交超时';
        } else {
            $rsp = curl_getinfo( $curl, CURLINFO_HTTP_CODE);
            if (200 !== $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($curl);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $curl );
        return $result;
    }





























}
